﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace INTROSE
{
    public partial class View_Transactions_Tabbed : Form
    {
        SqlConnection con = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True");
        private string name, position;
        private int num;
        public View_Transactions_Tabbed(string name, string position)
        {
            InitializeComponent();
            this.name = name;
            this.position = position;
            con.Open();
            SqlCommand command = new SqlCommand("select transNo from orders", con);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    comboTransNum.Items.Add(reader.GetInt32(0));
                }
            }
            con.Close();
        }

        void FillData(string query)
        {
            // 1
            // Open connection
            using (SqlConnection c = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True"))
            {
                c.Open();
                // 2
                // Create new DataAdapter
                using (SqlDataAdapter a = new SqlDataAdapter(query,c))
                {
                    // 3
                    // Use DataAdapter to fill DataTable
                    DataTable t = new DataTable();
                    a.Fill(t);
                    // 4
                    // Render data onto the screen
                    dataTrans.DataSource = t;
                }
                c.Close();
            }
        }

        private void comboViewBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string query;
            if (comboViewBy.Text == "Company")
            {
                comboList.Items.Clear();
                query = "SELECT * FROM orders order by transNo";
                FillData(query);
            }
            else if (comboViewBy.Text == "Date")
            {
                comboList.Items.Clear();
                comboList.Items.Add("Daily");
                comboList.Items.Add("Weekly");
                comboList.Items.Add("Monthly");
                comboList.Items.Add("Yearly");
            }
            else if (comboViewBy.Text == "Status")
            {
                comboList.Items.Clear();
                comboList.Items.Add("Paid");
                comboList.Items.Add("Unpaid");
                comboList.Items.Add("Claimed");
                comboList.Items.Add("Unclaimed");
                comboList.Items.Add("Delivered");
                comboList.Items.Add("Undelivered");
            }
            else if (comboViewBy.Text == "Mode of Requisition")
            {
                comboList.Items.Clear();
                comboList.Items.Add("Pick-up");
                comboList.Items.Add("Delivery");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int transNum, checker = 0;
            try
            { 
                num = Int32.Parse(txtTransNum.Text);
                con.Open();
                SqlCommand command = new SqlCommand("select transNo from orders", con);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        transNum = reader.GetInt32(0);
                        if (transNum == num)
                            checker = 1;
                    }
                }
                if (checker == 1)
                {
                    con.Close();
                    this.Close();
                    View_Transactions view = new View_Transactions(name, position, num);
                    view.Show();
                }
                else if (checker == 0)
                    MessageBox.Show("Transaction number is does not exist!");
            }
            catch (FormatException) { MessageBox.Show("Transaction number is invalid!"); }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (comboTransNum.Text == "")
                MessageBox.Show("Please choose a transaction number from the dropdown box.");
            else
            {
                num = Int32.Parse(comboTransNum.Text);
                this.Close();
                View_Transactions view = new View_Transactions(name, position, num);
                view.Show();
            }
        }

        private void btnMainMenu_Click(object sender, EventArgs e)
        {
            this.Close();
            if (position == "factory worker")
            {
                MenuProductionStaff menu = new MenuProductionStaff(name, position);
                menu.Show();
            }
            else
            {
                MenuExecutive menu = new MenuExecutive(name, position);
                menu.Show();
            }
        }

        private void comboList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string query;
            if (comboViewBy.Text == "Date")
            {
                
            }
            else if (comboList.Text == "Paid")
            {
                query = "SELECT * FROM orders where paid = 1";
                FillData(query);
            }
            else if (comboList.Text == "Unpaid")
            {
                query = "SELECT * FROM orders where paid = 0";
                FillData(query);
            }
            else if (comboList.Text == "Claimed")
            {
                query = "SELECT transNo FROM orders where claimed = 1";
                FillData(query);
            }
            else if (comboList.Text == "Unclaimed")
            {
                query = "SELECT transNo FROM orders where claimed = 0";
                FillData(query);
            }
            else if (comboList.Text == "Delivered")
            {
                query = "SELECT * FROM orders where delivered = 1";
                FillData(query);
            }
            else if (comboList.Text == "Undelivered")
            {
                query = "SELECT * FROM orders where delivered = 0";
                FillData(query);
            }
            else if (comboList.Text == "Pick-up")
            {
                query = "SELECT * FROM orders where modDistr = 0";
                FillData(query);
            }
            else if (comboList.Text == "Delivery")
            {
                query = "SELECT * FROM orders where modDistr = 1";
                FillData(query);
            }
        }
    }
}